#!/usr/bin/env python3
"""
停止后端应用
"""
import subprocess
import sys

def stop_backend():
    """停止后端应用"""
    try:
        # 查找并杀死Python进程
        result = subprocess.run(
            ["pgrep", "-f", "python.*app.main"],
            capture_output=True,
            text=True
        )
        
        if result.returncode == 0:
            pids = result.stdout.strip().split('\n')
            for pid in pids:
                if pid:
                    subprocess.run(["kill", "-9", pid])
                    print(f"已停止进程: {pid}")
        else:
            print("未找到运行中的后端进程")
            
        # 查找并杀死占用8000端口的进程
        result = subprocess.run(
            ["lsof", "-ti", ":8000"],
            capture_output=True,
            text=True
        )
        
        if result.returncode == 0:
            pids = result.stdout.strip().split('\n')
            for pid in pids:
                if pid:
                    subprocess.run(["kill", "-9", pid])
                    print(f"已停止端口8000进程: {pid}")
        else:
            print("未找到占用8000端口的进程")
            
    except Exception as e:
        print(f"停止应用时出错: {e}")

if __name__ == "__main__":
    stop_backend()